// pages/sign-log/list/list.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //当前页面
    current: 1,
    //每页的最大数
    size: 20,
    //总页面
    pages: 0,
    searchParams: {
      signId: "",
      userName: null,
      status: null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var signId = options.signId
    var searchParams = this.data.searchParams
    searchParams.signId = signId
    this.setData({
      signId: signId,
      searchParams: searchParams
    })
    this.getSignLogList()
  },

  getSignLogList: function (res) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var searchParams = this.data.searchParams
    searchParams.current = that.data.current
    searchParams.size = that.data.size
    wx.request({
      url: app.host + '/signLog/list',
      header: {
        accessToken: app.accessToken
      },
      data: searchParams,
      method: "post",
      complete: function (res) {
        wx.hideLoading()
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            that.setData({
              signLogList: res.data.data.records,
              pages: res.data.data.pages,
              current: res.data.data.current
            })
          }
        }
      }
    })
  },

  /** 标记 */
  sign: function (e) {
    var status = e.currentTarget.dataset.status
    this.setData({
      updateStatus: e.currentTarget.dataset.status
    })
    this.updateStatus()
    this.setData({
      modalName: null
    })
    console.log(e)
  },

  /** 搜索 */
  search: function (e) {
    console.log(e)
    var search = e.detail.value
    var searchParams = this.data.searchParams
    searchParams.userName = search
    this.setData({
      searchParams: searchParams
    })
    this.getSignLogList()
  },

  updateStatus: function (res) {
    var that = this
    wx.request({
      url: app.host + '/signLog/updateStatus',
      header: {
        accessToken: app.accessToken,
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        signLogId: that.data.updateId,
        status: that.data.updateStatus
      },
      method: "post",
      complete: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            that.getSignLogList()
          }
        }
      }
    })
  },


  /** 筛选状态 */
  filterStatus: function (e) {
    console.log(e)
    var status = e.currentTarget.dataset.status
    var searchParams = this.data.searchParams
    searchParams.status = status
    this.setData({
      searchParams: searchParams
    })
    this.getSignLogList()
    this.setData({
      modalName: null
    })
  },

  /** 控制筛选器的展示 */
  showFiltrate: function (e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },

  showModal(e) {
    //设置当前操作的对象
    console.log(e)
    var index = e.currentTarget.dataset.index
    var currentSignLog = this.data.signLogList[index]
    this.setData({
      modalName: e.currentTarget.dataset.target,
      currentSignLog: currentSignLog,
      updateId: currentSignLog.id,
      updateIndex: index
    })
  },
  hideModal(e) {
    console.log(e)
    this.setData({
      modalName: null
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})